package com.googlecode.lookie.platform.internal.log;

import java.util.HashSet;
import java.util.Set;

import com.googlecode.lookie.core.log.AbstractLog;
import com.googlecode.lookie.core.log.AbstractLogStore;
import com.googlecode.lookie.core.log.Log;
import com.googlecode.lookie.core.log.LogListener;
import com.googlecode.lookie.core.log.LogRecord;
import com.googlecode.lookie.core.log.LogStoreListener.ChangeType;

public class PlatformLog
    extends AbstractLogStore
    implements Log
{

    private DelegateLog delegate = new DelegateLog();

    private Set<LogRecord> records = new HashSet<LogRecord>();

    public void clean()
    {
        synchronized ( records )
        {
            records.clear();
        }
    }

    public LogRecord[] getRecords()
    {
        synchronized ( records )
        {
            return records.toArray( new LogRecord[records.size()] );
        }
    }

    public void log( LogRecord record )
    {
        synchronized ( records )
        {
            records.add( record );
        }
        notifyChange( ChangeType.ADDITION );

        delegate.log( record );
    }

    public void remove( LogRecord record )
    {
        synchronized ( records )
        {
            records.remove( record );
        }
        notifyChange( ChangeType.REMOVAL );
    }

    public void addLogListener( LogListener listener )
    {
        delegate.addLogListener( listener );
    }

    public void removeLogListener( LogListener listener )
    {
        delegate.removeLogListener( listener );
    }

    private class DelegateLog
        extends AbstractLog
    {

        public void log( LogRecord record )
        {
            notifyLogging( record );
        }

    }

}
